<?php

class ArticleActionController 
{ 
	private $view;
	private $args;


	public function setView(View $view)
    {
		$this->view = $view; 
		return $this;
	}

	public function setArgs($args = null) { 
		if(null != $args) { 
			$this->args = $args;
		}
		return $this->args; 
	}
	public function run() {
		if (isset($this->args['get']['id'])) {
				$mapperArticles = new MapperArtiles();
				$article = $mapperArticles->getOneArticle($this->args['get']['id']);
				$article->getTitle();
                $mapperComments = new MapperComments();
				if (empty($article)) {
					$error = 'Статья не найдена';
					$this->view->setError($error);
				} else {	
					$this->view->setArticles($article);
				}
				if (isset($this->args['post']['send'])) {
					$text = $this->args['post']['comment'];
					if (empty($text)) {
						$error = "Нельзя добавить пустой комментарий.";
						$this->view->setError($error);
					} else {
						$id = $article->getId();
						$create = $mapperComments->createComment($id, $text);
						if ($create) {
						header("Location: {$_SERVER['REQUEST_URI']}");	
						} else {
							$error = 'Ошибка при создании';
							$this->view->setError($error);
						}
					}
				}
			}
		$this->view->setRenderTemplate("article.php"); 
	}
}
